#include "vector_op.hpp"
#include <math.h>
#include <iostream>

void simple_stat(vectorf* v, vector* res)
{
    double x1=0.0;
    double x2=0.0;
    double m;
    int n;
    
    if( res->size() != 2 ) {
        res->resize(2);
    }
    n = v->size();
    for(int i=0;i<n;i++) {
        double val = v->get(i);
        x1 = x1 + val;
        x2 = x2 + val*val;
    }
    m = x1/n;
    res->set(0,m);
    res->set(1,sqrt(x2/n-m*m));
    
}


void linear_combine(vector* a, vector* b, vector*c, double beta, double gamma)
{
    if( ( a->size() != b->size() ) || (b->size() != c->size()) ) {
        std::cerr << "Size of vectors should be the same" << std::cerr;
        throw;
    }
    int n = a->size();
    double res;
    for(int i=0;i<n;i++) {
        res = beta*b->get(i)+gamma*c->get(i);
        a->set(i,res);
    }
}




void abs2(cvector* in, vector* out) {
    if( in->size() != out->size()  ) {
        std::cerr << "Size of vectors should be the same: " << in->size() << " " << out->size() << std::cerr;
        throw;
    }    
    int n = in->size();
    gsl_complex val;
    for(int i=0;i<n;i++) {
        val = in->get(i);
        out->set(i,val.dat[0]*val.dat[0]+val.dat[1]*val.dat[1]);
    }    
}